(function () {
  let pid = location.search.split("=")[1];
  let oDiv = document.querySelector("div");
  AG("/goods/item", { id: pid }).then((resData) => {
    console.log(resData.data);
    let goodsData = resData.data.info;
    oDiv.innerHTML = `
        <img src="${goodsData.img_big_logo}">
        <input type="button" value="加入购物车">
    `;
    //注意 获取按钮的操作放到这个回调函数里
    let addBtn = document.querySelector("input");
    addBtn.addEventListener("click", function () {
      AP(
        "/cart/add",
        { id: localStorage.getItem("uid"), goodsId: pid },
        true
      ).then((resData) => {
        console.log(resData.data);
        if (resData.data.code == 401 || resData.data.code == 0) {
          location.href = `login.html?returnUrl=${location.href}`;
        }
        if (resData.data.code == 1) {
          location.href = "cart.html";
        }
      });
    });
  });
})();
